package cn.com.doone.common.uc.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.doone.common.uc.utils.MessageUtils;
import cn.com.doone.common.uc.utils.PropertiesUtils;
import cn.com.doone.common.uc.utils.RedisUtils;
import cn.com.doone.common.uc.utils.StringUtils;
import cn.com.doone.common.uc.web.BaseController;
import cn.com.doone.common.uc.web.service.AppService;
import cn.com.doone.common.uc.web.service.SyncService;

@Controller
@RequestMapping("/sync")
public class SyncController extends BaseController {
	private static final Logger LOG = LoggerFactory.getLogger(SyncController.class);
	@Autowired
	private SyncService syncService;

	@Autowired
	private AppService appService;
	
	@Autowired
	private PropertiesUtils propertiesUtils;

	/**
	 * 查询组分页列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryUserGroupPageList",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryUserGroupPageList(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		if(StringUtils.isNull(paramsMap.get("page")) || StringUtils.isNull(paramsMap.get("limit"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		return syncService.queryGroupPageList(paramsMap);
	}
	
	/**
	 * 查询树形化组
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryUserGroupTreeList", method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryUserGroupTreeList(HttpServletRequest request) {
		Map<String,Object> paramsMap = getParams(request);
		return syncService.queryUserGroupTreeList(paramsMap);
	}

	/**
	 * 验证组编号
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/validGroupCode")
	public @ResponseBody Map<String, Object> validGroupCode(HttpServletRequest request) throws Exception {
		Map<String, Object> params = getParams(request);
		return syncService.validGroupCode(params);
	}

	/**
	 * 添加组
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addGroup", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> addGroup(HttpServletRequest request) {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		Map<String, Object> paramsMap = getParams(request);
		// 判断非空
		if (StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("groupName"))
				|| StringUtils.isNull(paramsMap.get("fkGroupId"))) {
			paramsMap.put("success", false);
			paramsMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return paramsMap;
		}
		try {
			// 比较加密
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			resultMap = syncService.mergeUserGroup(paramsMap);
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", e.getMessage());
			LOG.debug("添加组异常:" + e);
		}

		return resultMap;
	}

	/**
	 * 修改组
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/modifyGroup", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> modifyGroup(HttpServletRequest request) {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取参数
		Map<String, Object> paramsMap = getParams(request);

		try {
			// 判断参数非空
			if (StringUtils.isNull(paramsMap.get("userGroupId")) || StringUtils.isNull(paramsMap.get("clientId"))
					|| StringUtils.isNull(paramsMap.get("fkGroupId"))) {
				resultMap.put("success", false);
				resultMap.put("message", MessageUtils.PARAMS_NULL);
				LOG.info("传递的参数:" + paramsMap.toString());
				return resultMap;
			}
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			resultMap = syncService.mergeUserGroup(paramsMap);
		} catch (Exception e) {
			LOG.error("修改组异常:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "修改组异常");
		}

		return resultMap;

	}
	
	/**
	 * 启用禁用组
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/editGroupStatus", method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> editGroupStatus(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramsMap = getParams(request);
		if (StringUtils.isNull(paramsMap.get("userGroupId")) || StringUtils.isNull(paramsMap.get("status"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		resultMap = syncService.editGroupStatus(paramsMap);
		return resultMap;
	}

	/**
	 * 删除组
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/removeGroup", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> removeGroup(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramsMap = getParams(request);
		try {
			// 判断参数非空
			if (StringUtils.isNull(paramsMap.get("userGroupId")) || StringUtils.isNull(paramsMap.get("clientId"))
					|| StringUtils.isNull(paramsMap.get("authenticator"))) {
				resultMap.put("success", false);
				resultMap.put("message", MessageUtils.PARAMS_NULL);
				LOG.info("传递的参数:" + paramsMap.toString());
				return resultMap;
			}
			paramsMap.put("plainText", paramsMap.get("clientId"));

			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			resultMap = syncService.deleteUserGroup(paramsMap);
		} catch (Exception e) {
			LOG.error("删除组异常:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "删除组异常");
		}

		return resultMap;
	}
	
	/**
	 * 查询用户分页列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryUserPageList",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryUserPageList(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		if(StringUtils.isNull(paramsMap.get("page")) || StringUtils.isNull(paramsMap.get("limit"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		return syncService.queryUserPageList(paramsMap);
	}
	
	/**
	 * 根据组ID查询用户分页列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryUserPageListByUserGroupId",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryUserPageListByUserGroupId(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		if(StringUtils.isNull(paramsMap.get("page")) || StringUtils.isNull(paramsMap.get("limit")) || StringUtils.isNull(paramsMap.get("userGroupId"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		resultMap = syncService.queryUserPageListByUserGroupId(paramsMap);
		return resultMap;
	}
	
	/**
	 * 根据ID查询用户信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryUserInfoById",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryUserInfoById(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		if(StringUtils.isNull(paramsMap.get("userInfoId"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		resultMap = syncService.queryUserInfoById(paramsMap);
		return resultMap;
	}
	
	/**
	 * 禁用启用用户
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/editUserStatus",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> editUserStatus(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		if (StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("status"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		resultMap = syncService.editUserStatus(paramsMap);
		return resultMap;
	}

	/**
	 * 验证用户信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/validUserInfo", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> validUserInfo(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramsMap = getParams(request);
		// 判断非空
		if (StringUtils.isNull(paramsMap.get("userAccount")) && StringUtils.isNull(paramsMap.get("mobilePhone"))
				&& StringUtils.isNull(paramsMap.get("email"))) {
			resultMap.put("success", false);
			resultMap.put("message", "参数不能为空");
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}
		if (StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", "参数不能为空");
			return resultMap;
		}
		paramsMap.put("plainText", paramsMap.get("clientId"));
		try {
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			resultMap = syncService.validUserInfo(paramsMap);
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", "验证用户信息异常");
			LOG.error("验证用户信息异常:" + e);
		}
		return resultMap;
	}

	/**
	 * 添加用户
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addUser(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramsMap = getParams(request);

		// 判断非空
		if (StringUtils.isNull(paramsMap.get("clientId"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}
		paramsMap.put("plainText", paramsMap.get("clientId"));
		try {
			// 比较加密
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			// 添加用户
			resultMap = syncService.addUserInfo(paramsMap);
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", StringUtils.isNull(e.getMessage()) ? "添加用户异常" : e.getMessage());
			LOG.error("添加用户异常:" + e);
		}

		return resultMap;
	}

	/**
	 * 更新用户信息
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> updateUserInfo(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramsMap = getParams(request);
		if (StringUtils.isNull(paramsMap.get("token")) || StringUtils.isNull(paramsMap.get("authenticator"))
				|| StringUtils.isNull(paramsMap.get("clientId"))) {
			resultMap.put("success", false);
			resultMap.put("msg", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}
		paramsMap.put("plainText", paramsMap.get("clientId"));
		try {
			// 比较加密
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			// 根据token获取用户信息
			Map<String, Object> userInfoMap = syncService.queryInfoIdByToken(paramsMap);
			if (StringUtils.isNull(userInfoMap.get("userInfoId"))) {
				resultMap.put("success", false);
				resultMap.put("msg", "查无此用户");
				return resultMap;
			}
			paramsMap.put("userInfoId", userInfoMap.get("userInfoId"));
			resultMap = syncService.updateUserInfo(paramsMap);
			resultMap.put("success", true);
			resultMap.put("msg", MessageUtils.DO_SUCCESS);
		} catch (Exception e) {
			LOG.error("更新用户信息发生异常:" + e.getMessage());
			resultMap.put("success", false);
			resultMap.put("msg", "修改用户信息失败");
		}

		return resultMap;

	}

	/**
	 * 删除用户
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/removeUser", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> removeUser(HttpServletRequest request) {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramsMap = getParams(request);

		// 判断参数非空
		if (StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("clientId"))
				|| StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}

		paramsMap.put("plainText", paramsMap.get("clientId"));
		try {
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			resultMap = syncService.deleteUserInfo(paramsMap);
		} catch (Exception e) {
			LOG.error("删除用户异常：" + e);
			resultMap.put("success", false);
			resultMap.put("message", "删除用户异常");
		}

		return resultMap;

	}

	/**
	 * 修改用户
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/modifyUser")
	public @ResponseBody Map<String, Object> modifyUser(HttpServletRequest request) {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 获取参数
		Map<String, Object> paramsMap = getParams(request);

		// 判断参数非空
		if (StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("clientId"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}

		paramsMap.put("plainText", paramsMap.get("clientId"));

		try {
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.updateUserInfo(paramsMap);
		} catch (Exception e) {
			LOG.error("修改用户异常:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "修改用户异常");
			return resultMap;
		}
		return resultMap;
	}

	/**
	 * 更新用户账号
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/modifyUserAccount",method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> changeUserAccount(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取参数
		Map<String, Object> paramsMap = getParams(request);
		// 判断参数不为空
		if (StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("userAccount"))
				|| StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}
		// 验证加密
		paramsMap.put("plainText", paramsMap.get("clientId"));
		try {
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			resultMap = syncService.updateUserAccount(paramsMap);
		}catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.EDIT_FAIL);
			LOG.error("修改用户账号失败, 失败原因：" + e.getMessage());
		}
		return resultMap;
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/changePassword", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> changePassword(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取参数
		Map<String, Object> paramsMap = getParams(request);

		// 判断参数不为空
		if (StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("oldPassword"))
				|| StringUtils.isNull(paramsMap.get("newPassword")) || StringUtils.isNull(paramsMap.get("clientId"))
				|| StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}

		// 验证加密
		paramsMap.put("plainText", paramsMap.get("clientId"));
		try {
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			if(propertiesUtils.isCheckLock()) {
				// 验证IP是否被锁
				RedisUtils.validIp(request, resultMap);
				if (!resultMap.isEmpty()) {
					return resultMap;
				}
			}
			resultMap = syncService.changePasswordByUserInfoId(paramsMap);
			if ("false".equals(resultMap.get("success"))) {
				if(propertiesUtils.isCheckLock()) {
					RedisUtils.addIpList(request);
				}
				LOG.info("用户ID:" + paramsMap.get("userInfoId").toString() + "密码修改失败,失败原因:" + resultMap.get("message"));
			} else {
				LOG.info("用户id" + paramsMap.get("userInfoId").toString() + "密码修改成功");
			}
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.EDIT_FAIL);
			LOG.error("用户id" + paramsMap.get("userInfoId").toString() + "修改密码失败, 失败原因：" + e.getMessage());
		}
		return resultMap;

	}

	/**
	 * 密码重置
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> resetPassword(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取参数
		Map<String, Object> paramsMap = getParams(request);

		// 判断参数不为空
		if (StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("clientId"))) {

			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}

		try {
			// 比较加密
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			if(propertiesUtils.isCheckLock()) {
				// 验证IP是否被锁定
				RedisUtils.validIp(request, resultMap);
				if(!resultMap.isEmpty()) {
					return resultMap;
				}
			}

			resultMap = syncService.changePasswordByUserInfoId(paramsMap);
			if ("false".equals(resultMap.get("success"))) {
				if(propertiesUtils.isCheckLock()) {
					RedisUtils.addIpList(request);
				}
				LOG.info("用户ID:" + paramsMap.get("userInfoId").toString() + "重置密码失败,失败原因:" + resultMap.get("message"));
			} else {
				LOG.info("用户ID:" + paramsMap.get("userInfoId").toString() + "重置密码成功");
			}
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.EDIT_FAIL);
			LOG.error("用户id" + paramsMap.get("userInfoId").toString() + "重置密码失败, 失败原因：" + e.getMessage());
		}

		return resultMap;
	}
	
	/**
	 * 账号绑定其他应用
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/bindAppAndAccount",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> bindAppAndAccount(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		if (StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("clientId"))
				|| StringUtils.isNull(paramsMap.get("authAccount")) || StringUtils.isNull(paramsMap.get("appCode"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
		}
		resultMap = syncService.bindAppAndAccount(paramsMap);
		return resultMap;
	}

	/**
	 * 添加组用户
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addGroupMember", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> addGroupMember(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取参数
		Map<String, Object> paramsMap = getParams(request);
		if (StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("userGroupId"))
				|| StringUtils.isNull(paramsMap.get("clientId"))
				|| StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}

		try {

			// 比较加密
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.addUserGroupMember(paramsMap);

			resultMap.put("success", true);
			resultMap.put("message", MessageUtils.DO_SUCCESS);
			LOG.debug("添加组成员成功");
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.DO_FAIL);
			LOG.error("添加组成员失败：" + e);
		}
		return resultMap;
	}

	/**
	 * 删除组用户
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/removeGroupMember", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> removeGroupMember(HttpServletRequest request) {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 获取参数
		Map<String, Object> paramsMap = getParams(request);
		if (StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("userGroupId"))
				|| StringUtils.isNull(paramsMap.get("clientId"))
				|| StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}

		try {
			// 比对加密
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.deleteUserGroupMember(paramsMap);
			LOG.info("删除组成员成功");
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.DO_FAIL);
			LOG.error("删除组成员失败：" + e);
		}
		return resultMap;
	}

	/**
	 * 员工离职
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/demoted", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> demoted(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramsMap = getParams(request);
		if (StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("authenticator"))
				|| StringUtils.isNull(paramsMap.get("userInfoId")) || StringUtils.isNull(paramsMap.get("groupCode"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}
		paramsMap.put("plainText", paramsMap.get("clientId"));
		try {
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			resultMap = syncService.demoted(paramsMap);
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", "员工离职异常");
			LOG.error("员工离职异常:" + e);
		}
		return resultMap;
	}

	/**
	 * 获取角色
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getRoles", method = RequestMethod.POST)
	public Map<String, Object> getRoles(HttpServletRequest request) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Map<String, Object> paramsMap = getParams(request);

		if (StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("token"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}

		resultMap = syncService.queryRoles(paramsMap);

		return resultMap;
	}

	/**
	 * 根据手机号码查询用户信息
	 * 
	 * @author cjf
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryUserInfoByPhoneNum", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> queryUserInfoByPhoneNum(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramsMap = getParams(request);
		if (StringUtils.isNull(paramsMap.get("phoneNum")) || StringUtils.isNull(paramsMap.get("clientId"))
				|| StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}
		try {
			// 重新加密比对
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.queryUserInfoByPhoneNum(paramsMap);
		} catch (Exception e) {
			LOG.error("根据手机号码获取用户信息失败:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "根据手机号码查询用户信息失败");
		}
		return resultMap;
	}

	/**
	 * 根据账号查询用户信息
	 * 
	 * @author cjf
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryUserInfoByUserAccount", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> queryUserInfoByUserAccount(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramsMap = getParams(request);
		if (StringUtils.isNull(paramsMap.get("userAccount")) || StringUtils.isNull(paramsMap.get("clientId"))
				|| StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}
		try {
			// 重新加密比对
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.queryUserInfoByUserAccount(paramsMap);
		} catch (Exception e) {
			LOG.error("根据账号查询用户信息失败:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "根据账号查询用户信息失败");
		}
		return resultMap;
	}

	/**
	 * 账号解锁
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateLock", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> updateLock(HttpServletRequest request) {
		String username = request.getParameter("username");
		String ip = request.getParameter("IP");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			RedisUtils.delAccountList(username);
			RedisUtils.delIp(ip);
			resultMap.put("success", true);
			resultMap.put("message", "操作成功");
			LOG.info("redis删除异常账号和IP");
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", "操作异常:" + e);
			LOG.error("redis操作异常:" + e);
		}
		return resultMap;
	}
	
	/**
	 * 查询岗位列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryPostList",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryPostList(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		LOG.info("调用查询岗位列表queryPostList接口，接收的参数是:"+paramsMap);
		if(StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		try {
			// 重新加密比对
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.queryPostList(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("查询岗位列表失败:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "查询岗位列表失败");
		}
		return resultMap;
	}
	
	/**
	 * 根据岗位查看人员
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryUserListByPost",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryUserListByPost(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		LOG.info("调用根据岗位查看人员queryUserListByPost接口，接收的参数是:"+paramsMap);
		if(StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		try {
			// 重新加密比对
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.queryUserListByPost(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("根据岗位查看人员失败:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "根据岗位查看人员失败");
		}
		return resultMap;
	}
	
	/**
	 * 查询职能组列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryUserGroupList",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryUserGroupList(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		LOG.info("调用查询职能组列表queryUserGroupList接口，接收的参数是:"+paramsMap);
		if(StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		try {
			// 重新加密比对
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.queryUserGroupList(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("查询职能组列表失败:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "查询职能组列表失败");
		}
		return resultMap;
	}
	
	/**
	 * 查询组成员
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryUserGroupMemberList",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryUserGroupMemberList(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		LOG.info("调用查询组成员queryUserGroupMemberList接口，接收的参数是:"+paramsMap);
		if(StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("authenticator")) 
				|| StringUtils.isNull(paramsMap.get("userGroupId"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		try {
			// 重新加密比对
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.queryUserGroupMemberList(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("查询组成员失败:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "查询组成员失败");
		}
		return resultMap;
	}
	
	/**
	 * 查询人员信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryUserInfoList",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryUserInfoList(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		LOG.info("调用查询人员信息queryUserInfoList接口，接收的参数是:"+paramsMap);
		if(StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		try {
			// 重新加密比对
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.queryUserInfoList(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("查询人员信息失败:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "查询人员信息失败");
		}
		return resultMap;
	}
	
	/**
	 * 根据当前部门ID查询上级部门
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/querySuperGroupByGroupId",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> querySuperGroupByGroupId(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		LOG.info("调用根据当前部门查询上级部门querySuperGroupByGroupId接口，接收的参数是:"+paramsMap);
		if(StringUtils.isNull(paramsMap.get("userGroupId")) || StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		try {
			// 重新加密比对
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			resultMap = syncService.querySuperGroupByGroupId(paramsMap);
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("调用根据当前部门查询上级部门接口失败:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "根据当前部门查询上级部门");
		}
		
		return resultMap;
	}

	/**
	 * 统计应用用户是否有授权
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/isUserAppAuthz",method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> isUserAppAuthz(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		if(StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("userInfoId"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		resultMap = syncService.isUserAppAuthz(paramsMap);
		return resultMap;
	}
	
	/**
	 * 根据用户ID查询部门信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryGroupIdByUserInfoId", method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> queryGroupIdByUserInfoId(HttpServletRequest request) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> paramsMap = getParams(request);
		LOG.info("接口: 根据用户ID查询部门信息 接收的参数是:"+paramsMap);
		if(StringUtils.isNull(paramsMap.get("userInfoId"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		try {
			// 重新加密比对
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}

			resultMap = syncService.queryGroupIdByUserInfoId(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("根据用户ID查询部门失败:" + e);
			resultMap.put("success", false);
			resultMap.put("message", "查询部门信息失败");
		}
		return resultMap;
	}
	
	/**
	 * 同步数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/syncData", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> syncData(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 获取参数
		Map<String, Object> paramsMap = getParams(request);

		// 参数非空判断
		if (StringUtils.isNull(paramsMap.get("clientId")) || StringUtils.isNull(paramsMap.get("updateTime"))
				|| StringUtils.isNull(paramsMap.get("authenticator"))) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			LOG.info("传递的参数:" + paramsMap.toString());
			return resultMap;
		}

		try {
			// 重新加密对比
			paramsMap.put("plainText", paramsMap.get("clientId"));
			resultMap = appService.comparisonAuth(paramsMap);
			if (!resultMap.isEmpty()) {
				return resultMap;
			}
			resultMap = syncService.syncData(paramsMap);
		} catch (Exception e) {
			resultMap.put("success", false);
			resultMap.put("message", "同步数据失败");
			LOG.error("同步数据失败,失败原因:" + e);
		}

		return resultMap;
	}
}
